//
//  BN_ContentXQViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-21.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_ContentXQViewController.h"
#define MIN_X 15
#define CONTENT_WIDTH 290
#define TITLE_FONT 22
#define CONTENT_FONT 17

@interface BN_ContentXQViewController () {
    // 当前是幼儿园哪个版块
    NSInteger kinderSchoolSection;
    BN_KinderGartenHelper *helper_;
}

@end

@implementation BN_ContentXQViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // UI
    self.tabBarController.tabBar.hidden = YES;
    if (MAIN_VERSION < 7) {
        [[UIBarButtonItem appearance] setBackgroundImage: [UIImage new]                                                forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"详情页";
    
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backVc)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    if (MAIN_VERSION >= 7) {
        self.contentScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
    }else {
        self.contentScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    
    // 风火轮
    [self viewLoadHintView];
    
    // 帮助类
    helper_ = [[BN_KinderGartenHelper alloc]init];
    kinderSchoolSection = [[NSUserDefaults standardUserDefaults] integerForKey:USERDEFAULTS_KINDERSECTION];
    switch (kinderSchoolSection) {
        case INTRODUCTION:
            [self requestNetData_INTRODUCTION];
            break;
        case ACTIVITY:
            [self requestNetData_ACTIVITY];
            break;
        case FOOD:
            [self requestNetData_FOOD];
            break;
        case SUGGESTIONBOX:
            [self requestNetData_SUGGESTIONBOX];
            break;
        default:
            break;
    }
}

- (void)toGroup {
    // title
    self.titleLabel.text = self.titleStr;
    self.titleLabel.numberOfLines = 0;
    UIFont * tfont = [UIFont fontWithName:@"TrebuchetMS-Bold" size:TITLE_FONT];
    self.titleLabel.font = tfont;
    self.titleLabel.lineBreakMode = NSLineBreakByCharWrapping ;
    CGSize size =CGSizeMake(CONTENT_WIDTH,10000);
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize actualsize = [self getTextSize:size disFont:tfont contentStr:self.titleLabel.text contextDic:tdic];
    switch (MAIN_VERSION) {
        case 6:
            self.titleLabel.frame = CGRectMake(MIN_X, 20, CONTENT_WIDTH, actualsize.height);
            break;
        default:
            self.titleLabel.frame = CGRectMake(MIN_X, 20+64, CONTENT_WIDTH, actualsize.height);
            break;
    }

    // 时间
    self.timeLabel.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.titleLabel.frame)+5, CONTENT_WIDTH, 16);
    NSRange rang = NSMakeRange(0, 19);
    NSString * strRang = [self.timeStr substringWithRange:rang];
    self.timeLabel.text = strRang;
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    self.timeLabel.textColor = APP_TIME_COLOR;
    
    // 内容图片
    self.contImageView.clipsToBounds = YES;
    self.contImageView.contentMode = UIViewContentModeScaleAspectFill;
    if (kinderSchoolSection == SUGGESTIONBOX) {
        self.contImageView.frame = CGRectMake(10, CGRectGetMaxY(self.timeLabel.frame)+15, CONTENT_WIDTH, 0) ;
        
    }else {
        if (kinderSchoolSection == FOOD) {
            self.contImageView.frame = CGRectMake(MIN_X, 15, CONTENT_WIDTH, CONTENT_WIDTH);
        }else {
            self.contImageView.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.timeLabel.frame)+15, CONTENT_WIDTH, CONTENT_WIDTH);
        }
    }
    
    // 内容文字
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 4;
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:CONTENT_FONT], NSParagraphStyleAttributeName:paragraphStyle};
    self.contLabel.attributedText = [[NSAttributedString alloc]initWithString:_contStr attributes:attributes];
    self.contLabel.numberOfLines = 0;
    UIFont * contLabelfont = [UIFont systemFontOfSize:CONTENT_FONT];
    self.contLabel.font = contLabelfont;
    self.contLabel.lineBreakMode = NSLineBreakByCharWrapping ;
    CGSize contLabelsize = CGSizeMake(CONTENT_WIDTH,10000);
    NSDictionary * contLabeldic = [NSDictionary dictionaryWithObjectsAndKeys:contLabelfont,NSFontAttributeName,nil];
    CGSize contLabelActualsize = [self getTextSize:contLabelsize disFont:contLabelfont contentStr:self.contLabel.text contextDic:contLabeldic];
    
    // 内容Label
    if (kinderSchoolSection == SUGGESTIONBOX) {
        self.contLabel.frame = CGRectMake( MIN_X, CGRectGetMaxY(self.contImageView.frame)-5, CONTENT_WIDTH, contLabelActualsize.height);
    }else {
        self.contLabel.frame = CGRectMake( MIN_X, CGRectGetMaxY(self.contImageView.frame)+10, CONTENT_WIDTH, contLabelActualsize.height);
    }
    [self.contLabel sizeToFit];
    
    // UIScrollView
    // 速率
    [_contentScrollView setDecelerationRate:0.1];
    switch (MAIN_VERSION) {
        case 6:
            self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(self.contLabel.frame)+20+44);
            break;
        default:
            self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(self.contLabel.frame)+20);
            break;
    }
}

#pragma mark
#pragma mark 网络请求
- (void)requestNetData_INTRODUCTION {
    // code...
}

- (void)requestNetData_ACTIVITY {
    __weak typeof(self) weakSelf = self;
    [helper_ queryActionDetailById:self.objctId completeHandler:^(id result, NSString *errMsg) {
        
        //update by jason yan 2014-05-30
        if (errMsg) {
            [weakSelf cancelHintView];
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
        }
        if(result){
            [weakSelf cancelHintView];
            NSArray *infoArr = (NSArray *)result;
            // 内容、时间及其标题
            if (![infoArr[0][@"actiSubj"] isEqual:[NSNull null]]) {
                weakSelf.titleStr = infoArr[0][@"actiSubj"];
            }
            if (![infoArr[0][@"actiCont"] isEqual:[NSNull null]]) {
                weakSelf.contStr = infoArr[0][@"actiCont"];
            }
            weakSelf.timeStr = infoArr[0][@"actiDate"];
            // 图片URL
            
            NSArray *jianceArr = infoArr[0][@"files"];
            if (jianceArr.count > 0) {
                NSString *urlStr = infoArr[0][@"files"][0][@"fileTUrl"];
                urlStr = [THUMBNAIL_BASEURL stringByAppendingString:urlStr];
                weakSelf.imgUrl = [NSURL URLWithString:urlStr];
                [weakSelf.contImageView setImageWithURL:weakSelf.imgUrl];
            }
            [weakSelf toGroup];
        }
    }];
}

- (void)requestNetData_FOOD {
    
    __weak typeof(self) weakSelf = self;
    [helper_ queryFoodDetailById:self.objctId completeHandler:^(id result, NSString *errMsg) {
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            [weakSelf cancelHintView];
        }
        if (result) {
            [weakSelf cancelHintView];
            NSLog(@"美食详情：%@",result);
        }
    }];
}

- (void)requestNetData_SUGGESTIONBOX {
    __weak typeof(self) weakSelf = self;
    [helper_ querySuggestInfoById:self.objctId completeHandler:^(id result, NSString *errMsg) {
        
        //update by jason yan 2014-05-30
        if (errMsg) {
            [weakSelf cancelHintView];
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            [weakSelf cancelHintView];
        }

        if(result){
            [weakSelf cancelHintView];
            NSArray *infoArr = (NSArray *)result;
            // 内容、时间及其标题
            if (![infoArr[0][@"suggSubj"] isEqual:[NSNull null]]) {
                weakSelf.titleStr = infoArr[0][@"suggSubj"];
            }
            if (![infoArr[0][@"suggCont"] isEqual:[NSNull null]]) {
                weakSelf.contStr = infoArr[0][@"suggCont"];
            }
            weakSelf.timeStr = infoArr[0][@"suggDate"];
            
            [weakSelf toGroup];
        }
    }];
}

#pragma mark
#pragma mark 文本高度自适应
// 封装自动撑开高度的方法
- (CGSize)getTextSize:(CGSize)provisionsSize disFont:(UIFont *)font contentStr:(NSString *)contentStr contextDic:(NSDictionary *)contextDic {
    
    CGSize size;
    switch (MAIN_VERSION) {
        case 6:
            size = [contentStr sizeWithFont:font constrainedToSize:provisionsSize lineBreakMode:NSLineBreakByWordWrapping];
            break;
        default:
            size =[contentStr boundingRectWithSize:provisionsSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:contextDic context:nil].size;
            
            break;
    }
    return size;
}

#pragma mark
#pragma mark Action

- (void)releaseResource {
    helper_ = nil;
    self.titleStr = nil;
    self.imgUrl = nil;
    self.contStr = nil;
    self.timeStr = nil;
    self.objctId = nil;
}

- (void)backVc {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    NSLog(@"幼儿园详情页 dealloc...");
}
@end
